// pages/personal/myorderdetails/myorderdetails.js
var utils = require('../../../utils/util.js');
var QRCode = require('../../../utils/weapp-qrcode.js');
var qrcode;
var timeAll = "";

function countdown(that) {
  var EndTime = that.data.end_time || [];
  var NowTime = Date.parse(new Date()) / 1000;
  var total_micro_second = EndTime - NowTime || [];
  // console.log(total_micro_second);
  // 渲染倒计时时钟
  that.setData({
    clock: "还剩" + dateformat(total_micro_second) + "自动关闭"
  });
  if (total_micro_second <= 0) {
    that.setData({
      clock: "已经截止"
    });
    clearTimeout(timeAll);
    //return;
  }
  timeAll = setTimeout(function() {
    countdown(that);
  }, 1000)
}

// 时间格式化输出，如11:03 25:19 每1s都会调用一次
function dateformat(second) {
  // 总秒数
  // var second = Math.floor(micro_second / 1000);
  // 天数
  var day = Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24);
  // 分钟
  var min = Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60);
  return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";

}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isback: true,
    isok: true,
    end_time: '',
    create_timestamp: "" //订单创建开始时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.onOrderList(options.id);


  },
  onOrderList: function(id) {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/order/my_order_detail", {
      order_id: id
    }, function(res) {
      utils.hideLoading();
      var total_amount = 0;
      for (var key in res.datas.snap_items) {
        total_amount = total_amount + (res.datas.snap_items[key].order_price * res.datas.snap_items[key].num)
      }
      that.setData({
        info: res.datas,
        create_timestamp: res.datas.create_timestamp,
        total_amount: total_amount
      })

      //  qrcode.makeCode(res.datas.verification);  //用元素对应的code更新二维码
      qrcode = new QRCode('canvas', {
        text: res.datas.id + "@" + res.datas.verification,
        width: 104,
        height: 104,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H,
      });
      //获取结束时间
      var day = Math.floor((7 * 86400) + that.data.create_timestamp);
      86400 //一天的秒数
      that.setData({
        end_time: day
      })
      if (that.data.info.order_state != 8 && that.data.info.order_state != 4 && that.data.info.order_state != 7 && that.data.info.order_state != 6) {
        countdown(that);
      }


    })
    // 天数


  },
  //跳入物流进度
  onLogisticsProgressTap: function(e) {
    console.log(e)
    wx.navigateTo({
      url: '../logisticsprogress/logisticsprogress?orderid=' + e.currentTarget.dataset.orderid
    })
  },
  //跳入申请售后
  onAfterSaleTap: function(e) {
    wx.navigateTo({
      url: '../aftersale/aftersale?id=' + e.currentTarget.dataset.id,
    })
  },
  complaint: function(e) {
    wx.navigateTo({
      url: '/pages/personal/complaint/complaint?id=' + e.currentTarget.dataset.id,
    })
  },
  //取消订单
  onColseOrderTap: function(e) {
    var that = this;
    that.setData({
      isback: false
    })
  },
  //取消订单
  onOkOrderTap: function(e) {
    var that = this;
    utils.http("/api/v1/order/ok_order", {
      order_id: e.currentTarget.dataset.id
    }, function(res) {
      if (res.state == 1) {
        that.setData({
          isback: true,
          isok: false
        })
        // that.onOrderList(that.data.orderState)
      } else {
        utils.message(res.datas)
      }
    })
  },
  //点击去逛逛 跳入  分类
  onClassifyTap: function(e) {
    var that = this;
    wx.switchTab({
      url: '../../classify/classify'
    })

  },
  //关闭取消订单
  onCoseTap: function(e) {
    var that = this;
    that.setData({
      isback: true,
      isok: true
    })
  },
  //取消订单成功跳入订单状态页
  onCoseOrderTap: function(e) {
    wx.navigateBack({

    })
  },
  //订单评价
  onEvaluationTap: function(e) {
    var that = this;
    wx.navigateTo({
      url: '../../evaluation/evaluation?id=' + e.currentTarget.dataset.id,
    })
  },
  //确认收货
  onReceivingTap: function(e) {
    var that = this;
    wx.showModal({
      title: '确认收货',
      content: '您是确认收货吗',
      success(res) {
        if (res.confirm) {
          utils.http("/api/v1/order/ok_order", {
            order_id: e.target.dataset.id
          }, function(res) {
            if (res.state == 1) {
              that.setData({
                currentPage: 1,
                orderList: []
              })
              wx.navigateBack({

              })
            } else {
              utils.message(res.datas)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  //去付款
  goConfirMorderTap: function(e) {
    wx.navigateTo({
      url: '../../confirmorder/confirmorder',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //提醒发货
  deliveryTap: function(e) {
    var that = this;
    utils.http("/api/v1/order/ques_delivery", {
      order_id: e.currentTarget.dataset.id
    }, function(res) {
      if (res.state == 1) {
        wx.showToast({
          title: "已提醒成功",
          icon: "none"
        })
      } else {
        utils.message(res.datas)
      }
    })
  },
})